import ApiService from "@/utils/service";
import { ElMessage } from "element-plus";
import blogStore from '@/store/blog';
import { query } from "./db";
import { ret } from "@/type/response";

const request = new ApiService()
const blog = blogStore()

export async function login(email: string, password: string) {
  const loginStatus: ret = await request.post("/api/user/login", { email, password })
  if (!loginStatus.status) return ElMessage({ type: 'error', message: loginStatus.data })
  const { token, id } = loginStatus.data
  const userinfoRet: any = await query("userinfo", { id })
  blog.$patch(state => {
    state.token = token
    if (userinfoRet.status) state.userinfo = userinfoRet.data[0]
  })
}